<?php

// Question 部分
/**
 * $question 应该为数组，结构： header：标题和文字叙述题干，type:0选择题1判断题2主观题 resources：资源信息：将会展示在下方。 choises:选择题干（1 2 3 4为key以此类推，可以用作打乱顺序）。
 * $answer: type=0时，结构为answer:(choises下面的选项index) type=1时，answer为boolean值 type=2时，answer为正常的text。
 * $qexplain 应该为数组，结构：header为文字叙述解析，resources为资源信息，图片/视频等
 */
function addQuestion($uid, $sid, $description, $question, $answer, $view_level, $qexplain = array(), $similar = array(), $label = "", $answer_view_level = 1, $explain_view_level = 1)
{
    T("question_all_question")->insert(array(
        "uid" => $uid,
        "sid" => $sid,
        "description" => $description,
        "question" => json_encode($question),
        "answer" => $answer,
        "view_level" => $view_level,
        "qexplain" => json_encode($qexplain),
        "similar" => json_encode($similar),
        "label" => $label,
        "answer_view_level" => $answer_view_level,
        "explain_view_level" => $answer_view_level
    ), array(
        "uid",
        "sid",
        "view_level",
        "answer_view_level",
        "explain_view_level"
    ));
    return T("question_all_question")->getUniqueID();
}

function updateQuestion($questionid, $description, $question, $answer, $view_level, $qexplain = array(), $similar = array(), $label, $answer_view_level = 1, $explain_view_level = 1)
{
    T("question_all_question")->update(array(
        "description" => $description,
        "question" => json_encode($question),
        "answer" => $answer,
        "view_level" => $view_level,
        "qexplain" => json_encode($qexplain),
        "similar" => json_encode($similar),
        "label" => $label,
        "answer_view_level" => $answer_view_level,
        "explain_view_level" => $answer_view_level,
        "lastEditData" => "current_timestamp"
    ), "`questionid`=$questionid", array(
        "view_level",
        "answer_view_level",
        "explain_view_level",
        "lastEditData"
    ));
}

function getQuestion($questionid)
{
    $data = T("question_all_question")->fetch_by_questionid($questionid);
    if (! $data) {
        return array();
    }
    return dealQuestion($data);
}

function loadQuestionArray($questions)
{
    $back = array();
    foreach ($questions as $v) {
        $back[$v["questionid"]] = dealQuestion($v);
    }
    return $back;
}

function dealQuestion($data)
{
    $question = json_decode($data["question"], true);
    $data["question"] = $question;
    if (isset($question["appends"]["image"]))
        $data["question"]["appends"]["image"] = loadQuestionAppendArray($question["appends"]["image"]);
    if (isset($question["appends"]["video"]))
        $data["question"]["appends"]["video"] = loadQuestionAppendArray($question["appends"]["video"]);
    $answer = $data["answer"];
    if ($question["type"] == "0") {
        $data["answer_detial"] = $question["choises"][$answer];
    }
    if ($question["type"] == "1") {
        if ($answer == "1")
            $data["answer_detial"] = "正确";
        else
            $data["answer_detial"] = "错误";
    }
    $qexplain = json_decode($data["qexplain"], true);
    $data["qexplain"] = $qexplain;
    if (isset($qexplain["appends"]["image"]))
        $data["qexplain"]["appends"]["image"] = loadQuestionAppendArray($qexplain["appends"]["image"]);
    if (isset($qexplain["appends"]["video"]))
        $data["qexplain"]["appends"]["video"] = loadQuestionAppendArray($qexplain["appends"]["video"]);
    return $data;
}

function loadQuestionAppendArray($array)
{
    $back = array();
    if (! $array) {
        return array();
    }
    foreach ($array as $v) {
        $data = getResourcePathByRid($v);
        if ($data) {
            $back[$v] = $data;
        }
    }
    return $back;
}

function countQuestionByUid($uid)
{
    return T("question_all_question")->count_something("`uid`=$uid");
}

function getQuestionCard($qid)
{
    $callback = getQuestion($qid);
    if (! $callback) {
        return array();
    }
    $back = array();
    $back["type"] = $callback["label"];
    $back["description"] = $callback["description"];
    $back["card"] = getCardInfo($callback["uid"]);
    $back["similar"] = $callback["similar"];
    $back["subject"] = getSubjectBySid($callback["sid"]);
    return $back;
}

function dealQuestionCard($qid)
{
    $callback = getQuestion($qid);
    if (! $callback) {
        return array();
    }
    $back = array();
    $back["type"] = $callback["label"];
    $back["description"] = $callback["description"];
    $back["card"] = getCardInfo($callback["uid"]);
    $back["similar"] = $callback["similar"];
    $back["subject"] = getSubjectBySid($callback["sid"]);
    return $back;
}

function question_search($uid, $index = 0, $amount = 10, $view_level = -1, $orderby = "createdate", $description = "-1", $label = "-1")
{
    $where = "`uid` = $uid ";
    if ($view_level != - 1)
        $where .= "AND `view_level` = $view_level ";
    if ($description == "-1") {
        if ($label != - 1)
            $where .= "AND concat(`label`) like '%$label%' ";
    } else {
        $where .= "AND concat(`description`) like '%$description%' ";
    }
    $where .= "ORDER BY $orderby DESC";
    return loadQuestionArray(T("question_all_question")->fetch_question_by_search($where, $index, $amount));
}

/**
 * type=0:全部
 * type=1:superQuestion
 * type=2:explain
 * type=3:answer
 */
function checkPermission($uid, $qid, $type = 0, $isData = false)
{
    $ques = null;

    if (! $isData)
        $ques = getQuestion($qid);
    else
        $ques = $qid;
    if (! $ques)
        return false;
    if ($ques["uid"] == $uid)
        return true;
    if ($type == 0) {
        if ($ques["view_level"] == 1)
            return getTeacherSubject($uid, false) == $ques["sid"];
        if ($ques["view_level"] == 2)
            return true;
    }
    if ($type == 1) {
        return true;
    }
    if ($type == 2)
        return $ques["answer_view_level"] == 1;
    if ($type == 3)
        return $ques["explain_view_level"] == 1;
}

// QuestionComment部分

// QuestionCollection部分
?>